/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

fileListGrants = "assign";

var singleListSelectionData = [[1,_('Translator')],[2,_('Reviewer')]];
var singleListStatusData = [[1,DEFTRANS[FILE_STATUS_TRANS]],[2,DEFTRANS[FILE_STATUS_PROOF]]];

Ext.extend(Verbum.list.AssignWindow, Ext.util.Observable, {

    show : function(){
    	var scope = this;
    	var desktop = scope.desktop;
	    var formwin='filelistAssign_'+scope.filePid;
		var fileListEditWin = desktop.getWindow(formwin);
				
		if (!fileListEditWin) {
						
		    var fileListForm = new Ext.form.FormPanel({
		        url: scope.url,
		        labelWidth: 100,
		        height:140,
		        waitMsgTarget: true,
		        frame: true,
		        items: [{
		        	    xtype : "fieldset",
						autoHeight : true,
						items : [
							{
								xtype: 'combo',
								fieldLabel: _('Assign myself as'),
								hiddenName: 'type',
								name: 'assigntype',
								id: 'assigntype',
								emptyText: _('Assign myself as'),
								editable: false,
								forceSelection: true,
								triggerAction: 'all',
							   	store: singleListSelectionData,
							   	mode: 'local',
							   	width: 170,
							   	allowBlank: false,
							   	blankText: _('The field is required.')
							},{
								xtype: 'combo',
								fieldLabel: _('Set status to'),
								hiddenName: 'initialstatus',
								name: 'initialstatus',
								emptyText: _('(optional)'),
								editable: false,
								forceSelection: true,
								triggerAction: 'all',
							   	store: singleListStatusData,
							   	mode: 'local',
							   	width: 170,
							   	allowBlank: true,
					        	vtype: 'checkList',
					        	vtypeText: _('The status is not valid according to your role.'),
					        	cmpField: 'assigntype',
							},{
								xtype: 'textarea',
								fieldLabel: _('Comments'),
								name: 'comment',
								width: 170,
								maxLength: 255,
								maxLengthText: _('The maximum length for this field is {0}')
							}]
		        }], 
				tbar: [{
					text: _('Save'),
					tooltip: _('Save'),
					iconCls: 'save',
					handler: function() {
							var selections = scope.fileList.selModel.getSelections();
						  	var encoded_array = "[";
						  	for(i = 0; i< scope.fileList.selModel.getCount(); i++){
						  		var ss = "{\"id\":\"" +selections[i].get('id') + "\",\"hash\":\"" + selections[i].get('chstamp') + "\"}";
								if (i==0) {
									encoded_array = encoded_array + ss;
								} else {
									encoded_array = encoded_array + "," + ss;
								}
						  	}
						  	encoded_array = encoded_array + "]";
	
		                	fileListForm.initFields();
	                   		if (fileListForm.getForm().isValid()) {
	                   			
	                    		fileListForm.getForm().submit({ 
		                        	method:'POST', 
		                        	waitTitle: _('Saving...'), 
		                        	waitMsg: _('Saving...'),
									params: {ids: encoded_array, pid: scope.filePid, op: 'single_assign'},
			                        success:function(form, action){
			                        	obj = Ext.util.JSON.decode(action.response.responseText);
			                        	if (obj.ret!=undefined) scope.fireEvent("refresh",obj.ret.results);
			                        	else scope.fireEvent("refresh",[]);
			                        	fileListEditWin.close();
			                        },
			                        failure:function(form, action){ 
		                                obj = Ext.util.JSON.decode(action.response.responseText); 
		                                Ext.Msg.alert(_('Error'), obj.error);
			                        	if (obj.ret!=undefined) scope.fireEvent("refresh",obj.ret.results);
			                        	else scope.fireEvent("refresh",[]);
		                                fileListEditWin.close(); 
			                        }                   	
			                    });
		                    }
						}
					}]		        	
			});        	
			
		    fileListEditWin = desktop.createWindow({
	            id: formwin,
	            layout:'fit',
	            title:_('Assign Files'),
	            width:350,
	            height:220,
	            resizable: false,
	            maximizable: false,
	            items: fileListForm
		    });
/*		    
		    fileListEditWin = new Ext.Window({
	            id: formwin,
	            layout:'fit',
	            title:_('Assign Files'),
	            width:350,
	            height:200,
	            resizable: false,
	            maximizable: false,
	            items: fileListForm
		    });
		    	*/
			fileListEditorWindows[0] = fileListEditWin;
 
		}
		fileListEditWin.show();        
    }
});